<template>
  <div>
    <template>
      <el-table
        :data="tableData"
        stripe
        style="width: 100%; font-size: 16px"
        height="550"
        size="small"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="goods_id" label="商品Id" width="80">
        </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="180">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格"> </el-table-column>
        <el-table-column prop="goods_number" label="商品数量">
        </el-table-column>
        <el-table-column prop="cat_one_id" label="商品一级分类id">
        </el-table-column>
        <el-table-column prop="cat_one_id" label="商品二级分类id">
        </el-table-column>
        <el-table-column prop="cat_one_id" label="商品三级分类id">
        </el-table-column>
        <el-table-column prop="goods_small_logo" label="商品图片">
          <template slot-scope="scope">
            <img :src="scope.row.goods_small_logo" height="80px" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="delsingle(scope.row)" type="text" size="medium"
              >删除</el-button
            >
            <el-button type="text" size="medium" @click="editGoods(scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-top: 20px">
        <el-button @click="delselected">删除选中</el-button>
      </div>
    </template>

    <template>
      <div class="block">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          layout="total,  prev, pager, next, jumper"
          :total="res.total"
          :page-size="10"
          background
        >
        </el-pagination>
      </div>
    </template>
    <el-form
      v-show="isShow"
      :model="editdata"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item class="editGoods"> 商品修改 </el-form-item>
      <el-form-item label="商品名称" prop="goods_name">
        <el-input v-model="editdata.goods_name" clearable> </el-input>
      </el-form-item>
      <el-form-item label="商品价格" prop="goods_price">
        <el-input v-model="editdata.goods_price"></el-input>
      </el-form-item>
      <el-form-item label="商品数量" prop="goods_number">
        <el-input v-model="editdata.goods_number"></el-input>
      </el-form-item>
      <el-form-item label="商品重量" prop="goods_weight">
        <el-input v-model="editdata.goods_weight"></el-input>
      </el-form-item>
      <el-form-item label="商品分类" required class="goodsclass">
        <el-row>
          <el-col :span="8">
            <el-form-item label="一级" prop="cat_one_id">
              <el-select
                v-model="editdata.cat_one_id"
                filterable
                placeholder="请选择一级分类"
              >
                <el-option
                  :label="item.cat_one_id"
                  :value="item.cat_one_id"
                  v-for="item in classOne"
                  :key="item.goods_id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="二级" prop="cat_two_id">
              <el-select
                v-model="editdata.cat_two_id"
                filterable
                placeholder="请选择二级分类"
              >
                <el-option
                  :label="item.cat_two_id"
                  :value="item.cat_two_id"
                  v-for="item in classTwo"
                  :key="item.goods_id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="三级" prop="cat_three_id">
              <el-select
                v-model="editdata.cat_three_id"
                filterable
                placeholder="请选择三级分类"
              >
                <el-option
                  :label="item.cat_three_id"
                  :value="item.cat_three_id"
                  v-for="item in classThree"
                  :key="item.goods_id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="审核状态" prop="goods_state">
        <el-radio-group v-model="editdata.goods_state">
          <el-radio :label="0">未通过</el-radio>
          <el-radio :label="1">审核中</el-radio>
          <el-radio :label="2">已审核</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否促销" prop="is_promote">
        <el-radio-group v-model="editdata.is_promote">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确定</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [],
      res: "",
      currentPage: 1,
      listPage: 1,
      isShow: false,
      classOne: [],
      classTwo: [],
      classThree: [],
      editdata: {
        goods_name: "",
        goods_price: "",
        goods_number: "",
        goods_weight: "",
        cat_one_id: "",
        cat_two_id: "",
        cat_three_id: "",
        is_promote: "",
        goods_state: "",
      },
      rules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" },
        ],
        goods_number: [
          { required: true, message: "请输入商品数量", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" },
        ],
        cat_one_id: [
          { required: true, message: "请选择一级分类", trigger: "change" },
        ],
        cat_two_id: [
          { required: true, message: "请选择二级分类", trigger: "change" },
        ],
        cat_three_id: [
          { required: true, message: "请选择三级分类", trigger: "change" },
        ],
        goods_state: [
          { required: true, message: "请选择审核状态", trigger: "change" },
        ],
        is_promote: [
          { required: true, message: "请选择促销状态", trigger: "change" },
        ],
      },

      multipleSelection: [],
    };
  },
  created() {
    this.getDate(1);
    this.Levelone();
    this.Leveltwo();
    this.Levelthree();
  },
  computed: {
    goodsInfo() {
      return this.$store.state.goodsInfo;
    },
  },
  methods: {
    handleCurrentChange(val) {
      this.getDate(val);
      this.listPage = val;
    },
    getDate(page) {
      this.$request
        .get("/getAllgoods", {
          params: { limit: 10, page: page },
        })
        .then(({ data }) => {
          this.res = data;
          this.tableData = data.data;
        });
    },
    //获取商品分类
    async Levelone() {
      const { data } = await this.$request.get("/category", {
        params: { classLevel: "one" },
      });
      this.classOne = data.data;
    },
    async Leveltwo() {
      const { data } = await this.$request.get("/category", {
        params: { classLevel: "two" },
      });
      this.classTwo = data.data;
    },
    async Levelthree() {
      const { data } = await this.$request.get("/category", {
        params: { classLevel: "three" },
      });
      this.classThree = data.data;
    },

    delsingle(x) {
      console.log(x);
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$request
            .post("/delshop", {
              goods_id: [x.goods_id],
            })
            .then(({ data }) => {
              this.getDate(this.listPage);
            });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delselected() {
      if (this.multipleSelection.length != 0) {
        this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            const idArr = this.multipleSelection.map((item) => {
              return item.goods_id;
            });
            console.log(idArr);
            this.$request
              .post("/delshop", {
                goods_id: idArr,
              })
              .then(({ data }) => {
                this.getDate(this.listPage);
              });
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      }
    },
    editGoods(y) {
      this.editdata.goods_name = y.goods_name;
      this.editdata.goods_price = y.goods_price;
      this.editdata.goods_number = y.goods_number;
      this.editdata.goods_weight = y.goods_weight;
      this.editdata.cat_one_id = y.cat_one_id;
      this.editdata.cat_two_id = y.cat_two_id;
      this.editdata.cat_three_id = y.cat_three_id;
      this.editdata.is_promote = y.is_promote;
      this.editdata.goods_state = y.goods_state;
      this.editdata.goods_id = y.goods_id;
      this.isShow = true;
    },
    //取消修改
    cancel() {
      this.isShow = false;
    },

    editTip(msg) {
      this.$alert(msg, "", {
        confirmButtonText: "确定",

        // callback: (action) => {
        //   this.$message({
        //     type: "info",
        //     message: `action: ${action}`,
        //   });
        // },
      });
      this.isShow = false;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert("submit!");
          const upd_time = new Date().getTime();
          this.$request
            .post("/updateGoods", {
              goods_name: this.editdata.goods_name,
              goods_price: this.editdata.goods_price,
              goods_number: this.editdata.goods_number,
              goods_weight: this.editdata.goods_weight,
              upd_time,
              cat_one_id: this.editdata.cat_one_id,
              cat_two_id: this.editdata.cat_two_id,
              cat_three_id: this.editdata.cat_three_id,
              is_promote: this.editdata.is_promote,
              goods_state: this.editdata.goods_state,
              goods_id: this.editdata.goods_id,
            })
            .then(({ data }) => {
              console.log(data);
              if (data.status === 200) {
                this.editTip("商品修改成功");
              } else {
                this.editTip("商品修改失败");
              }
              this.getDate(this.listPage);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //重置修改表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style scoped lang='scss'>
.el-table-column {
  overflow: scroll;
}
.block {
  margin-left: 30%;
}
.demo-ruleForm {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
  background: #ccc;
  padding: 20px 20px 20px 0px;
  z-index: 9999;
  .editGoods {
    text-align: center;
    font-weight: 700;
    ::v-deep .el-form-item__content {
      font-size: 30px;
    }
  }
  .goodsclass {
    ::v-deep .el-form-item__content {
      margin-left: 0px;
    }
  }
}
.el-scrollbar__view {
  z-index: 999;
}
.demo-ruleForm {
  z-index: 998;
}
</style>

